<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends WST_Model {
	private $_table = "user";

	function __contruct(){
        parent::__construct();
    }
	public function checkLogin($data=array()){
		$this->db->where('username = ', $data['username']);
		$this->db->where('password = ', MD5($data['password']));
		$this->db->where('status = ', 1);
		$query = $this->db->get($this->_table);

		if($query->num_rows()>0){
			return $query->row_array();
		}else {
			return FALSE;
		}
	}
	public function getByUserName($username){
		$query = $this->db->get_where ( $this->_table, array ('username' => $username) );
		if ($query->num_rows ()) {
			return $query->row_array ();
		}
		return false;
	}
	function getByEmail($email) {
		$query = $this->db->get_where ( $this->_table, array ('email' => $email ) );
		if ($query->num_rows ()) {
			return $query->row_array ();
		}
		return false;
	}
	public function insert($data=array()){
		if($this->db->insert($this->_table,$data)){
			return $this->db->insert_id();
		}
		return FALSE;
	}

	function active_user($code){
		$user = $this->getByCode ( $code );
		if ($user) {
			$forgotCode = md5 ( $user ['email'] . date ( "dmyhis" ) );
			$user ['forgot_code'] = $forgotCode;
			$user ['active_code'] = 1;
			$user ['status'] = 1;
			$this->db->update($this->_table, $user,array('id'=>$user['id']));
		}
		return $user;
	}

	function reset_password($code) {
		$user = $this->getByCodes ( $code );
		if ($user) {
			$password = md5 ( "RVHN" . date ( "YmdHis" ) );
			$password = substr ( $password, 0, 6 );
			$user ['password'] = md5 ( $password );
			$forgotCode = md5 ( $user ['email'] . date ( "dmyhis" ) );
			$user ['forgot_code'] = $forgotCode;
			$this->db->update($this->_table, $user, array('id'=>$user['id']));
			$user ['new_password'] = $password;
		}
		return $user;
	}

	//reset password
	function getByCodes($code) {
		$query = $this->db->get_where ( $this->_table, array ('forgot_code' => $code ) );
		if ($query->num_rows ()) {
			return $query->row_array ();
		}
		return false;
	}

	//active user
	function getByCode($code) {
		$query = $this->db->get_where ( $this->_table, array ('active_code' => $code ) );
		if ($query->num_rows ()) {
			return $query->row_array ();
		}
		return false;
	}
	function getSingle($user_id,$flag=false){
        $getUser = $this->cache->get('getByUser_'.$user_id);
		if($getUser===false || $flag===true){
            $this->db->where('id',$user_id);
            $query = $this->db->get($this->_table);
            $getUser = $query->row_array();
            $query->free_result();
            $this->cache->save('getByUser_'.$user_id, $getUser);
        }
		return $getUser;
	}

    public function count_all($param=array()){
        if(count($param)>0){
            if(!empty($param['keyword'])){
                $this->db->like('username',$param['keyword']);
            }
            if(!empty($param['email'])){
                $this->db->where('email', $param['email']);
            }
            if(!empty($param['parent'])){
                $this->db->where('parent', $param['parent']);
            }
            if(!empty($param['child'])){
                $this->db->where('parent', $param['child']);
                $this->db->where('level', $param['parent']);
            }
        }
        return $this->db->count_all_results($this->_table);
    }

    function find_all($param=array(),$current_page = 1, $page_size = 30){
        $where =" WHERE 1=1";
        if(count($param)>0){
            if(!empty($param['keyword'])){
                $where.=" AND username LIKE '%".$param['keyword']."%'";
            }
            if(!empty($param['email'])){
                $where.=" AND email = '".$param['email']."'";
            }
            if(!empty($param['parent'])){
                $where.=" AND parent = ".$param['parent']."";
            }
            if(!empty($param['child'])){
                $where.=" AND  parent = ".$param['child']." AND level= ".$param['parent']." ";
            }
        }
        $sql = "select * from ".$this->_table. $where."   order by id DESC";

        if ($current_page > 0 && $page_size > 0) {
            $offset = ($current_page-1)* $page_size;
            $sql .= " limit {$offset}, {$page_size}";
        }
        $query = $this->db->query($sql);
        $data = $query->result_array();
        $query->free_result();
        return $data;
    }

    public function update($data,$id){
        $this->db->where('id',$id);
        $this->db->update($this->_table,$data);
        $this->getSingle($id,true);
        return true;
    }

}
